package com.zz.util;

import com.zz.node.ListNode;

import java.util.HashSet;
import java.util.Set;

public class ListNodeUtil {
    /**
     * 打印ListNode节点
     * @param node
     */
    public static void printListNode(ListNode node) {
        if(node == null) {
            return;
        }
        System.out.print(node.val + "\t");
        printListNode(node.next);
    }

    /**
     * 根据数组返回链表
     * @param arr
     * @return
     */
    public static ListNode getListNodeForArr(int[] arr){
        ListNode head = new ListNode();
        ListNode node = head;
        for (int num : arr) {
            node.next = new ListNode();
            node = node.next;
            node.val = num;
        }
        node = null;
        return head.next;
    }

    /**
     * 反转链表
     * @param head
     * @return
     */
    public static ListNode reverseListNode(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode tempList = reverseListNode(head.next);
        head.next.next = head;
        head.next = null;
        return tempList;
    }

    /**
     * 追加链表
     * @param head 原始链表
     * @param append 追加的链表内容
     * @return 最终链表
     */
    public static ListNode appendListNode(ListNode head, ListNode append){
        if(head == null){
            return append;
        }
        ListNode node = head;
        while (node.next != null){
            node = node.next;
        }
        node.next = append;
        return head;
    }
}
